package com.pvdnc.nsocket.server.handler;

import com.pvdnc.nsocket.log.SLog;
import com.pvdnc.nsocket.protocol.EnInterpreter;
import com.pvdnc.nsocket.protocol.Interpreter;
import com.pvdnc.nsocket.protocol.PacketCodec;
import com.pvdnc.nsocket.protocol.StreamInterpreter;
import com.pvdnc.nsocket.protocol.base.Packet;
import com.pvdnc.nsocket.server.BaseServer;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public abstract class SimpleServerHandler<S extends BaseServer,I extends Packet> extends SimpleChannelInboundHandler<I> {
    private static final SLog LOG=new SLog(SimpleServerHandler.class);

    private final S mServer;

    private String mCommand;

    public S getServer() {
        return mServer;
    }

    public SimpleServerHandler(S server, String commmand){
        this(server,new EnInterpreter(commmand,server.getFullEncrypt()));
    }

    public SimpleServerHandler(S server){
        mServer=server;
    }

    public <T extends Interpreter> SimpleServerHandler(S server,T interpreter){
        mServer=server;
        PacketCodec.getInstance().addInterpreter(interpreter);
        mCommand=interpreter.getCommand();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, I msg) throws Exception {
        LOG.d("received:"+mCommand+"\n\t"+msg);
        onReceived(ctx, msg);
    }

    public abstract void onReceived(ChannelHandlerContext ctx,I msg);


}
